import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/blindbox/blindbox_products_detail_vo.dart';

class BlindBoxDetailPageController extends GetxController {

  var data = BlindboxProductsDetailVo(
    data: BlindboxProductsData(
      id: 0, 
      images: [], 
      title: '', 
      price: '', 
      sales: 0, 
      status: 0, 
      remainingTime: 0, 
      rewardRules: '', 
      description: '', 
      isPurchase: 0, 
      endTime: '', 
      integral: 0, 
      lotteryTime: '',
      subtitle: '',
      tips: '',
      statusStr: '',
      orderSn: '',
      isRenewal: 0
    ),
    meta: BlindboxProductsMeta(
      status: []
    )
  ).obs;

  int blindBoxId = 0;
  
  @override
  void onReady() {
    Map<String, dynamic> mapArguments = Get.arguments;
    blindBoxId = mapArguments['id'];
    getBlindboxProductDetail();
    super.onReady();
  }

  /// 获取盲盒详情
  getBlindboxProductDetail({ bool showLoading = true }) async {
    BlindboxProductsDetailVo blindboxProductsDetailVo = await HttpUtilService.getBlindboxProductDetail(id: blindBoxId, loading: showLoading);
    data.value = blindboxProductsDetailVo;
    update();
  }

  /// 预约赎回
  redeem() async {
    var res = await HttpUtilService.redeem(orderSn: data.value.data!.orderSn!);
    if (res) {
      EasyLoading.showToast('赎回成功');
      getBlindboxProductDetail(showLoading: false);
    }
  }

  /// 继续预约
  continueAppointment() async {
    var res = await HttpUtilService.continueAppointment(orderSn: data.value.data!.orderSn!);
    if (res) {
      EasyLoading.showToast('续约成功');
      getBlindboxProductDetail(showLoading: false);
    }
  }
}